package com.hefei.api.customer.manager.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hefei.api.customer.client.CustomerUrlConstants;
import com.hefei.api.customer.manager.IUserManager;
import com.hefei.api.customer.vo.SysMenu;
import com.hefei.api.customer.vo.User;
import com.hefei.api.customer.vo.easyui.PageHelper;
import com.hefei.common.exception.ClientException;
import com.hefei.common.http.client.ClientFactory;
import com.hefei.common.http.request.RequestHead;
import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.http.util.SendRequest;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;


public class UserManager implements IUserManager{
	
	private static Logger logger = Logger.getLogger(UserManager.class);

	@Override
	public User login(String username)throws ClientException {
		
		long timer = System.currentTimeMillis();
		logger.info("login time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("username", username);
			String result = SendRequest.send(CustomerUrlConstants.CUSTOMER_SERVER_LOGIN_URL, dataJson, timer, head);
			logger.info("login time:" + timer + " result:" + result + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String objStr = JacksonUtil.convertToJsonStrs((Map<String, Object>) baseResponse.getReturnObject());
				User user = JacksonUtil.jsonToBean(objStr, User.class);
				return user;
			}else{
				return null;
			}
			
		} catch (Exception e) {
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		
		
	}

	@Override
	public String getUsernameById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SysMenu> getMenuByUserId(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> getDatagrid() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long getDatagridTotal(User user, Integer sysid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> datagridUser(PageHelper page, Integer sysid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void editUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUser(int id) {
		// TODO Auto-generated method stub
		
	}

}
